<?php
class keu_penerimaan_kas_bank_model extends MY_Model {
	var $model = 'keu_penerimaan_kas_bank';
	
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT trans.*, ktr.nama_kantor, cc.nama_cost_center, TO_CHAR(trans.tgl_transaksi,'DD/MM/YYYY') tgltrans
					  FROM keu_trans_kas_bank trans, 
							sw_kantor ktr, 
							sw_cost_center cc
					 WHERE     trans.db_cr = 'D'
						   AND ktr.kd_kantor = trans.kd_kantor
						   AND cc.id_cost_center_pk = trans.kd_cost_center
						   AND trans.tgl_transaksi BETWEEN
							TO_DATE('".post('date_from')."', 'DD/MM/YYYY') AND 
							TO_DATE('".post('date_to')."', 'DD/MM/YYYY') AND 
							(
								UPPER(ktr.nama_kantor) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(trans.kd_valuta) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(trans.keterangan) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(trans.id_dok) LIKE UPPER('%".post('keyword')."%') OR
								UPPER(cc.nama_cost_center) LIKE UPPER('%".post('keyword')."%')
							) 
							AND trans.id_dok IN (SELECT ID_REFF
												  FROM keu_transaksi
												 WHERE kd_tipe_jurnal = 'JKB01')
					ORDER BY ".(post('sort')=='' ? 'id_dok' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	
	function GetDetailJSON($as_doc='')
	{
		$ls_sql	= "	SELECT	*
					FROM	keu_trans_kas_bank_detail
					where id_dok = '".$as_doc."'";
		$result	= $this->db->query($ls_sql);			
		return json_encode( $result->row_array() );
	}

	
	function Add($arr_data){
		$this->db->insert('keu_trans_kas_bank', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('keu_trans_kas_bank', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_code){
		$result = $this->db->get_where('keu_trans_kas_bank', array('id_dok' => $as_code));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	

	
	function GetSingleView($as_code)
	{
		$ls_sql	= "SELECT    D.ID_DOK, D.ID_REFF, D.KD_KANTOR, 
				   TO_CHAR(D.tgl_transaksi,'DD/MM/YYYY') tgl_transaksi, D.KD_COST_CENTER, D.ID_COA, 
				   D.JML_DANA, D.KETERANGAN, D.STATUS_TRANSAKSI, 
				   D.DB_CR, D.KD_VALUTA, D.KURS, 				   
				    D.KD_BANK, COA.KETERANGAN NAMA_BANK
                    FROM    KEU_TRANS_KAS_BANK D  
                     INNER JOIN KEU_KAS_BANK_COA COA ON COA.ID_COA =D.ID_COA      
					WHERE	ID_DOK = '".$as_code."'";
							
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	
	
	function DeleteSingle($as_code)
	{
		try {
			
			if (intval($this->get_data("SELECT 1 FROM keu_transaksi WHERE id_reff = '".$as_code."' AND NVL(status_posting,'N')='Y'")) == 0){
				$this->db->delete('keu_transaksi_detail', array('id_dok' => $this->get_data("SELECT id_dok FROM keu_transaksi where id_reff='".$as_code."'")));
				$this->db->delete('keu_transaksi', array('id_reff' => $as_code));

				$this->db->delete('keu_trans_kas_bank_detail', array('id_dok' => $as_code));
				$this->db->delete('keu_trans_kas_bank', array('id_dok' => $as_code));
			}
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
}
?>